import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Dialogs 1.3
import boan.qt.FileSaver 1.0

Page {
    property var arrMs: []

    Rectangle {
        width: 100
        height: 100
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.leftMargin: 20
        anchors.topMargin: 200
        border.width: 2
        color: "darkgray"
        radius: 30
        z: 10
        Behavior on color { PropertyAnimation { duration: 150;} }
        Behavior on scale { PropertyAnimation { duration: 150;} }
        Image {
            id: btnAdd
            width: 60
            height: 60
            anchors.centerIn: parent
            source: "qrc:/imgs/plus2.png"
        }

        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onPressed: {parent.scale = 1.2; parent.color="white";}
            onReleased: {parent.scale = 1.0; parent.color = "darkgray";dlg.open();}
        }
    }

//    MouseArea {
//        anchors.fill: parent
//        z: 11

//        onClicked: {
//            sliderWeight.opacity = 0.0
//            cxx.opacity = 1
//            mouse.accepted = false
//            print("mainclick")
//            print("mouseX: ", mouseX, "   mouseY: ", mouseY)
//        }
//        onPressAndHold: {
//            print("pressandhold")
//            mouse.accepted = false
//        }

//        propagateComposedEvents: true
//    }



    ListView {
        id: cxx
        spacing: 20
        anchors.fill: parent
        anchors.leftMargin: 150
        anchors.rightMargin: 5

        Behavior on opacity { PropertyAnimation { duration: 200}}

        delegate: LineZhuLan {
            lineNum: model.index+1
            arrModel: model.ms
            showIndicator: false
            onSigClicked: {
                print(zhulanId)
                var lineIndex = zhulanId.split("_")[0]-""
                var zhulanIndex = zhulanId.split("_")[1]-""
                setWeight(lineIndex, zhulanIndex, weight)
            }
        }

        model: ListModel {
            id: lmodel
//            ListElement{lineNum:1;}
//            ListElement{lineNum:2}
//            ListElement{lineNum:3}
//            ListElement{lineNum:4}
//            ListElement{lineNum:5}
//            ListElement{lineNum:6}
//            ListElement{lineNum:7}
//            ListElement{lineNum:8}
        }

    }





    FileSaver {
        id: fs
    }


    Dialog {
        id: dlg
        visible: false
        title: "添加线路"
        width: 400
        height: 300
        onVisibleChanged: {
            slider.opacity = 0
        }

        modality: Qt.ApplicationModal

        contentItem:  Rectangle {
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    slider.opacity = 0
                }
            }

            Row {
                Column {
                    spacing: 10
                    width: 280
                    height: 400

                    Rectangle {
                        width: 200
                        height: 50
                    }

                    Row {
                        Text {
                            width: 140
                            font.pixelSize: 20
                            font.bold: true
                            horizontalAlignment: Text.AlignRight
                            text: "线路名称:"
                        }
                        Rectangle {
                            width: 140
                            height: 24
                            border.width: 2
                            Text {
                                anchors.fill: parent
                                font.pixelSize: 20
                                anchors.topMargin: 3
                                horizontalAlignment: Text.AlignHCenter
                                clip: true
                                text: lmodel.count+1+"号线"
                            }
                        }

                    }

                    Row {
                        Text {
                            width: 140
                            font.pixelSize: 20
                            font.bold: true
                            horizontalAlignment: Text.AlignRight
                            text: "猪栏个数:"
                        }
                        Rectangle {
                            width: 140
                            height: 24
                            border.width: 2
                            Text {
                                id: zhulan
                                text: "20"
                                anchors.fill: parent
                                anchors.topMargin: 3
                                font.pixelSize: 20
                                horizontalAlignment: Text.AlignHCenter
                                clip: true

                                MouseArea {
                                    anchors.fill: parent
                                    onClicked: {
                                        slider.which = "zhulan"
                                        slider.minVal = 1
                                        slider.maxVal = 50
                                        slider.setValue(zhulan.text-"")
                                        slider.opacity = 1.0
                                        slider.playAni()
                                    }
                                }
                            }
                        }
                    }

                    Row {
                        Text {
                            width: 140
                            font.pixelSize: 20
                            font.bold: true
                            horizontalAlignment: Text.AlignRight
                            text: "预设饲料:"
                        }
                        Rectangle {
                            width: 100
                            height: 24
                            border.width: 2
                            //                    TextInput {
                            //                        anchors.fill: parent
                            //                        font.pixelSize: 20
                            //                        horizontalAlignment: TextInput.AlignHCenter
                            //                        clip: true
                            //                        selectByMouse: true
                            //                        baselineOffset: 15
                            //                    }

                            Text {
                                id: siliao
                                anchors.fill: parent
                                anchors.topMargin: 3
                                font.pixelSize: 20
                                horizontalAlignment: Text.AlignHCenter
                                text: "50"

                                MouseArea {
                                    anchors.fill: parent

                                    onClicked: {
                                        slider.which = "siliao"
                                        slider.minVal = 1
                                        slider.maxVal = 100
                                        slider.setValue(siliao.text-"")
                                        slider.opacity = 1.0
                                        slider.playAni()
                                    }
                                }
                            }
                        }
                        Text {
                            width: 40
                            font.pixelSize: 20
                            font.bold: true
                            text: "斤"
                        }
                    }
                }

                ValueSlider {
                    id: slider
                    width: 100
                    height: 200
                    visible: opacity !== 0
                    opacity: 0
                    minVal: 10
                    maxVal: 200

                    Behavior on opacity { PropertyAnimation { duration: 150}}
                    onSigValue: {
                        if (which === "siliao") {
                            siliao.text = v
                        } else {
                            zhulan.text = v
                        }
                    }
                }
            }

            Row {
                anchors.bottom: parent.bottom
                anchors.horizontalCenter: parent.horizontalCenter
                Rectangle {
                    id: btnNo
                    width: 200
                    height: 50
                    border.width: 1
                    color: "darkgray"
                    Text {
                        font.pixelSize: 20
                        font.bold: true
                        anchors.centerIn: parent
                        text: "取消"
                    }
                    MouseArea {
                        anchors.fill: parent
                        hoverEnabled: true
                        onEntered: parent.color = "white"
                        onExited: parent.color = "darkgray"
                        onPressed: parent.color = "darkgray"
                        onReleased: {
                            parent.color = "white"
                            dlg.close()
                        }
                    }
                }

                Rectangle {
                    id: btnYes
                    width: 200
                    height: 50
                    border.width: 1
                    color: "darkgray"
                    Text {
                        font.pixelSize: 20
                        font.bold: true
                        anchors.centerIn: parent
                        text: "确定"
                    }
                    MouseArea {
                        anchors.fill: parent
                        hoverEnabled: true
                        onEntered: parent.color = "white"
                        onExited: parent.color = "darkgray"
                        onPressed: parent.color = "darkgray"
                        onReleased: {
                            parent.color = "white"
                            dlg.close()

                            var ms = []
                            for (var i=0; i<(zhulan.text-''); ++i) {
                                var vTmp = {}
                                vTmp.weight = siliao.text
                                vTmp.feedState = "notfed"
                                ms.push(vTmp)
                            }
                            var oneModel = {}
                            oneModel.ms = ms
                            lmodel.append(oneModel)

                            arrMs.push(oneModel)
                            fs.write(arrMs)
                        }
                    }
                }
            }

        }
    }


    function setWeight(lineIndex, zhulanIndex, weight) {
        arrMs[lineIndex-1].ms[zhulanIndex-1].weight = weight
//        print(JSON.stringify(arrMs))
//        lmodel.clear()
//        arrMs.forEach(i => {lmodel.append(i)})
        fs.write(arrMs)
    }

    Component.onCompleted: {
        arrMs = fs.getFeedInfo()
        arrMs.forEach(i => {lmodel.append(i)})
    }


}
